/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "cJSON.h"
#include "leaker_detector.h"
#include "iot_connect_log.h"
#include "sle_bridge_errors.h"
#include "timer_mgr.h"
#include "iotc_oh_sle.h"
#include <string>

namespace OHOS {
namespace iotConnect {

int32_t LeakDetector::DevInfoGetCharState(const IotcCharState inData, char **data, uint32_t *len) const
{
    int32_t res = SLE_BRIDGE_FAILED;

    cJSON *json = cJSON_ParseWithLength(inData.data, inData.len);
    if(nullptr == json) {
        HILOGE("create json error");
        return -1;
    }

    if (cJSON_AddStringToObject(json, "allSvcFlag", "0") == NULL) {
        cJSON_Delete(json);
        HILOGE("add brand error");
        return SLE_BRIDGE_ERR_ADD_JSON_DATA;
    }

    char* tempData = cJSON_PrintUnformatted(json);
    res = IotcOhSleSendCustomSecData(inData.devId, (uint8_t *)tempData, strlen(tempData));
    if(res != SLE_BRIDGE_SUCCESS) {
        HILOGE("IotcOhSleSendCustomSecData error, res: %{public}d", res);
        return SLE_BRIDGE_FAILED;
    }

    HILOGI("send message success, msgId: %{public}s", inData.msgId);
    TimerMgr::GetInstance()->SubThingModelWaitReply(inData);
    return SLE_BRIDGE_SUCCESS;
}

int32_t LeakDetector::ConfigurationManagementGetCharState(const IotcCharState inData, char **data, uint32_t *len)
{
    return SLE_BRIDGE_SUCCESS;
}

int32_t LeakDetector::ConfigurationManagementPutCharState(const IotcCharState inData, const char *data, uint32_t len)
{
    return SLE_BRIDGE_SUCCESS;
}

int32_t LeakDetector::InternalHandler(const IotcCharState inData, char **out, uint32_t *len, ThingModelPutOrGet flag)
{
    auto iter = mapThingModelSvcLeaker.find(std::string(inData.svcId));
    if(mapThingModelSvcLeaker.end() == iter) {
        HILOGE("not find svrId, svrId:%{public}s", inData.svcId);
        return SLE_BRIDGE_ERR_INVALID_PARAM;
    }

    SvcType type = static_cast<SvcType>(iter->second);
    switch (type)
    {
    case SvcType::DEV_INFO:
    {   
        this->DevInfoGetCharState(inData, out, len);
        break;
    }
    default:
        HILOGE("not find svrId, svrId:%{public}s", inData.svcId);
        break;
    }
    return SLE_BRIDGE_SUCCESS;
}

int32_t LeakDetector::GetAllInfoHandler(const std::string& devId, IotcCharState *state)
{
    int32_t ret = SLE_BRIDGE_FAILED;

    cJSON *json = cJSON_CreateObject();
    if (json == NULL) {
        HILOGE("create obj error");
        return SLE_BRIDGE_ERR_CREATE_JSON;
    }

    if (cJSON_AddStringToObject(json, "allSvcFlag", "1") == NULL) {
        cJSON_Delete(json);
        HILOGE("add brand error");
        return SLE_BRIDGE_ERR_ADD_JSON_DATA;
    }

    IotcCharState iotcState;
    iotcState.devId = devId.c_str();
    iotcState.data = cJSON_PrintUnformatted(json);
    iotcState.len = strlen(iotcState.data);

    ret = IotcOhSleSendCustomSecData(devId.c_str(), (uint8_t *)iotcState.data, iotcState.len);
    if(ret != SLE_BRIDGE_SUCCESS) {
        HILOGE("IotcOhSleSendCustomSecData error, ret: %{public}d", ret);
        cJSON_Delete(json);
        return SLE_BRIDGE_FAILED;
    }

    HILOGI("success send message to sub dev, msgId");
    TimerMgr::GetInstance()->SubThingModelWaitReply(std::move(iotcState));
    cJSON_Delete(json);

    return SLE_BRIDGE_SUCCESS;
}

// int32_t LeakDetector::ServiceListGetCharState(const IotcServiceInfo *svc, char **data, uint32_t *len)
// {
//     // if (data == NULL || *data != NULL) {
//     //     HILOGE("param invalid");
//     //     return SLE_BRIDGE_ERR_INVALID_PARAM;
//     // }

//     // cJSON *json = cJSON_CreateObject();
//     // if (json == NULL) {
//     //     HILOGE("create obj error");
//     //     return SLE_BRIDGE_ERR_CREATE_JSON;
//     // }

//     // if (cJSON_AddNumberToObject(json, "gasLeakEscalationValue", leakDetectorServiceList_.gasLeakEscalationValue) == NULL) {
//     //     cJSON_Delete(json);
//     //     HILOGE("add num error");
//     //     return SLE_BRIDGE_ERR_ADD_JSON_DATA;
//     // }

//     // if (cJSON_AddNumberToObject(json, "statusIndicator", leakDetectorServiceList_.statusIndicator) == NULL) {
//     //     cJSON_Delete(json);
//     //     HILOGE("add num error");
//     //     return SLE_BRIDGE_ERR_ADD_JSON_DATA;
//     // }

//     // *data = cJSON_PrintUnformatted(json);
//     // cJSON_Delete(json);
//     // if (*data == NULL) {
//     //     HILOGE("json print error");
//     //     return SLE_BRIDGE_ERR_PRINT_JSON;
//     // }
//     // *len = strlen(*data);
//     return SLE_BRIDGE_SUCCESS;
// }

// int32_t LeakDetector::ServiceListPutCharState(const IotcServiceInfo *svc, const char *data, uint32_t *len)
// {
//     // if (data == NULL || len == 0) {
//     //     return SLE_BRIDGE_ERR_INVALID_PARAM;
//     // }
//     // cJSON *json = cJSON_Parse(data);
//     // if (json == NULL) {
//     //     HILOGE("Parse json error");
//     //     return SLE_BRIDGE_ERR_PARSE_JSON;
//     // }

//     // cJSON *item = cJSON_GetObjectItem(json, "statusIndicator");
//     // if (item == NULL || !cJSON_IsBool(item)) {
//     //     cJSON_Delete(json);
//     //     return SLE_BRIDGE_ERR_GET_JSON_DATA;
//     // }

//     // if (item->type == cJSON_True) {
//     //     // TODO: 需要添加具体业务,与下挂设备进行通信
//     //     sleBaseFactoryReset_.resetAllSettings = true;
//     // } else {
//     //     // TODO: 需要添加具体业务,与下挂设备进行通信
//     //     sleBaseFactoryReset_.resetAllSettings = false;
//     // }
//     // cJSON_Delete(json);
//     return SLE_BRIDGE_SUCCESS;
// }
}
}
